<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>RPG 游戏开发（4）绘制地板和NPC并实现碰撞检测 | 麦甜怪圈 Xiaomai Circle</title><meta name="author" content="小麦 Andrew Xiaomai"><meta name="copyright" content="小麦 Andrew Xiaomai"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="ffffff"><meta name="description" content="首先，我们将 Atlas 素材里一开始我们准备好的 player_1 素材拖曳到 Hierarchy 面板。这时候就会产生第二个角色。我们在 Hierarchy 面板中选中刚刚加入的 player_1，然后在 Inspector 面板中，最顶端的 player_1 改为 Test_NPC。以区分他是玩家或是 NPC，然后我们将他的坐标（Position）的 X 轴修改为 -0.2，让他出现在玩家左">
<meta property="og:type" content="article">
<meta property="og:title" content="RPG 游戏开发（4）绘制地板和NPC并实现碰撞检测">
<meta property="og:url" content="https://kingsmai.github.io/2021/08/09/RPG-%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91%EF%BC%884%EF%BC%89%E7%BB%98%E5%88%B6%E5%9C%B0%E6%9D%BF%E5%92%8CNPC%E5%B9%B6%E5%AE%9E%E7%8E%B0%E7%A2%B0%E6%92%9E%E6%A3%80%E6%B5%8B/index.html">
<meta property="og:site_name" content="麦甜怪圈 Xiaomai Circle">
<meta property="og:description" content="首先，我们将 Atlas 素材里一开始我们准备好的 player_1 素材拖曳到 Hierarchy 面板。这时候就会产生第二个角色。我们在 Hierarchy 面板中选中刚刚加入的 player_1，然后在 Inspector 面板中，最顶端的 player_1 改为 Test_NPC。以区分他是玩家或是 NPC，然后我们将他的坐标（Position）的 X 轴修改为 -0.2，让他出现在玩家左">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://kingsmai.github.io/img/covers/Unity2020rpg2d.jpg">
<meta property="article:published_time" content="2021-08-09T12:51:08.000Z">
<meta property="article:modified_time" content="2021-08-09T12:51:08.000Z">
<meta property="article:author" content="小麦 Andrew Xiaomai">
<meta property="article:tag" content="Unity 游戏开发">
<meta property="article:tag" content="RPG 游戏">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://kingsmai.github.io/img/covers/Unity2020rpg2d.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://kingsmai.github.io/2021/08/09/RPG-%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91%EF%BC%884%EF%BC%89%E7%BB%98%E5%88%B6%E5%9C%B0%E6%9D%BF%E5%92%8CNPC%E5%B9%B6%E5%AE%9E%E7%8E%B0%E7%A2%B0%E6%92%9E%E6%A3%80%E6%B5%8B/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: {"limitDay":365,"position":"top","messagePrev":"这篇文章已经","messageNext":"天未被更新了，可能里边的内容已经过时，请注意斟酌。"},
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":200},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: 小麦 Andrew Xiaomai","link":"链接: ","source":"来源: 麦甜怪圈 Xiaomai Circle","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体中文","cht_to_chs":"你已切换为简体中文","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"bottom-left"},
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: true,
  islazyload: false,
  isAnchor: true,
  percent: {
    toc: true,
    rightside: true,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'RPG 游戏开发（4）绘制地板和NPC并实现碰撞检测',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-08-09 20:51:08'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', 'ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          const now = new Date()
          const hour = now.getHours()
          const isNight = hour <= 6 || hour >= 18
          if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
          else if (t === 'light') activateLightMode()
          else activateDarkMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><link rel="stylesheet" href="https://at.alicdn.com/t/c/font_4366094_hx3hbrje2st.css"><meta name="generator" content="Hexo 7.0.0"><link rel="alternate" href="/atom.xml" title="麦甜怪圈 Xiaomai Circle" type="application/atom+xml">
<link rel="alternate" href="/rss2.xml" title="麦甜怪圈 Xiaomai Circle" type="application/rss+xml">
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://avatars.githubusercontent.com/u/23566051?s=400&amp;u=cc8834557b9fe7ca3db3adc89606ae47503e603d&amp;v=4" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">54</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">37</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">28</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 文章</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fa-fw fas fa-list"></i><span> 列表</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/covers/Unity2020rpg2d.jpg')"><nav id="nav"><span id="blog-info"><a href="/" title="麦甜怪圈 Xiaomai Circle"><img class="site-icon" src="/img/logos/Logo-11.jpg"/><span class="site-name">麦甜怪圈 Xiaomai Circle</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 文章</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fa-fw fas fa-list"></i><span> 列表</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">RPG 游戏开发（4）绘制地板和NPC并实现碰撞检测</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-08-09T12:51:08.000Z" title="发表于 2021-08-09 20:51:08">2021-08-09</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-08-09T12:51:08.000Z" title="更新于 2021-08-09 20:51:08">2021-08-09</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E9%A2%86%E5%9F%9F/">领域</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF/">技术</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E9%A2%86%E5%9F%9F/%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91/">游戏开发</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/C-Sharp/">C-Sharp</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF/Unity-2020/">Unity 2020</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%B8%B8%E6%88%8F%E7%B1%BB%E5%9E%8B/">游戏类型</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%B8%B8%E6%88%8F%E7%B1%BB%E5%9E%8B/%E5%9C%B0%E7%89%A2%E6%8E%A2%E9%99%A9%E7%B1%BB%E6%B8%B8%E6%88%8F/">地牢探险类游戏</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">2.3k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>8分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="RPG 游戏开发（4）绘制地板和NPC并实现碰撞检测"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>首先，我们将 Atlas 素材里一开始我们准备好的 player_1 素材拖曳到 Hierarchy 面板。这时候就会产生第二个角色。我们在 Hierarchy 面板中选中刚刚加入的 player_1，然后在 Inspector 面板中，最顶端的 player_1 改为 Test_NPC。以区分他是玩家或是 NPC，然后我们将他的坐标（Position）的 X 轴修改为 -0.2，让他出现在玩家左侧。</p>
<p>接下来，在 Inspector 面板中，点击 Add Component 按钮，添加一个 BoxCollider2D 元件，该元件就是用来检测物体碰撞，碰撞包括：触碰到NPC和其对话、触碰到怪物造成伤害、触碰到墙体无法继续前进等等。调整框的大小，这里我选择和我的玩家一样。 调整参数为：Offset X: 0, Y: -0.01，Size X: 0.1, Y: 0.1</p>
<p><img src="/uploads/20210808%20Create%20NPC.png"/></p>
<h2 id="制作地板"><a href="#制作地板" class="headerlink" title="制作地板"></a>制作地板</h2><p>我们点击 Atlas，然后在 Inspector 面板中点击 Sprite Editor。我们找到两个合适的图标作为地板和墙体。我选择了深色的块作为墙壁，并将其命名为 wall_0；然后选择另一个较为浅色的块作为地板，将其命名为 floor_0。切割完毕之后，点击 Apply，这时候发现 Atlas 图集的小箭头多出了两个图标。</p>
<div style="display: flex; justify-content: space-between;">
  <img src="/uploads/20210808%20Wall%20Sprite.png"/>
  <img src="/uploads/20210808%20Floor%20Sprite.png"/>
</div>

<p>当我们将 floor_0 图块直接拖拽到 Hierarchy 面板上，默认他会出现在原点（x = 0, y = 0），也就是我们玩家的位置。这时候你会发现，我们的玩家被这个图块挡住了。</p>
<h2 id="修改渲染图层"><a href="#修改渲染图层" class="headerlink" title="修改渲染图层"></a>修改渲染图层</h2><p>为了解决这个问题，我们需要知道一个概念：图层。在 Hierarchy 面板选择 player_0，在 Inspector 面板定位到 Sprite Renderer 元件的 Sorting Layer 项（如果没有这个属性，则是隐藏在 Additional Layer 里）点开下拉框，然后点击 Add Sorting Layer。这时候 Inspector 面板将会跳转至 Tags &amp; Layer，并且展开 Sorting Layers 属性列。</p>
<p>在 Sorting Layer 点击右侧的 ＋ 号，新建一个图层 Actor（角色）。添加完毕后，我们再点击 Hierarchy 面板中的 player_0，再找到 Inspector 面板里的 Sorting Layer。点开下拉框选择刚刚添加的 Actor，然后你的 玩家就会出现在地板前面了！</p>
<p>同理，对 NPC 也要执行同样的操作。</p>
<p><img src="/uploads/20210808%20Sorting%20Layer.png"/></p>
<p>接下来，我们需要再继续复制粘贴几个地板，然后再把墙壁放上去。结果大概这样（如果你有强迫症，每个区块之间的间隔为0.16）我们现在用比较笨的方法去弄这个，后续将会使用 Tile Palette 功能进行地图绘制。</p>
<p><img src="/uploads/20210808%20Simple%20Map.png"/></p>
<h2 id="碰撞的概念"><a href="#碰撞的概念" class="headerlink" title="碰撞的概念"></a>碰撞的概念</h2><p>首先我们要知道几个需求，地板是没有 BoxCollider 的，所以不被考虑在内：</p>
<ol>
<li>玩家不可经过踩上去的地方（碰到这些，玩家会被阻拦）<ol>
<li>墙体</li>
<li>NPC</li>
</ol>
</li>
<li>玩家可以踩上去的地方（触发某些机制）<ol>
<li>开关</li>
<li>回复泉水</li>
<li>陷阱</li>
</ol>
</li>
</ol>
<p>所以，我们需要增加两个图层（Layer）。这和 Sorting Layer 是不同的，Sorting Layer 是关于渲染的层级关系，而 Layer 可以理解为一个组别，我们需要实现的是：当玩家碰上这些块的时候，会出现什么事情。</p>
<p>现在，我们选择 Hierarchy 界面的 wall_0 然后在 Inspector 界面的 Layer 下拉菜单中点击 Add Layer。我们在 User Layer 中依次添加 Blocking 和 Actor 层。</p>
<p><img src="/uploads/20210808%20User%20Layer.png"/></p>
<p>点击 Hierarchy 面板中的 wall_0，然后按住 CTRL 选择 wall_1（可以多选）。如果某些需要选择的物体在 Hierarchy 面板中是连续的，可以直接长按 Shift 多选。然后 Layer 下拉菜单中选择 Blocking，这将作为我们玩家的障碍物。然后我们也要给他俩加上 BoxCollider2D 元件。</p>
<p><img src="/uploads/20210808%20Wall%20Properties.png"/></p>
<p>之后再选择 player_0 和 Test_NPC，将 Layer 切换为刚刚创建的 Actor 层。</p>
<p>别担心，之后不会那么麻烦。我们会把这些琐碎的事情交给 Tilemap 来做，但是现在为了让大家理解原理，就先搞得麻烦一点。</p>
<p>有了碰撞体之后，还是不能阻止玩家经过的。我们需要让角色知道“哦，这是墙体，我不能穿过去”。那要让角色知道的事情，就需要用代码来完成了。</p>
<h2 id="编程"><a href="#编程" class="headerlink" title="编程"></a>编程</h2><p>碰撞检测的编程思路是这样的：我们需要在移动的时候，往移动的方向前方投影一个看不见的方块，如果这个方块检测到障碍物，那么人物就不能向前移动。</p>
<p>所以我们需要声明 RaycastHit2D hit; 变量来检测。</p>
<p>我们在之前朝向切换和开始移动之间写上一段代码，具体位置在这里：</p>
<figure class="highlight c#"><table><tr><td class="code"><pre><span class="line"><span class="keyword">if</span> (moveDelta.x &gt; <span class="number">0</span>)</span><br><span class="line">&#123;</span><br><span class="line">    transform.localScale = Vector3.one;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">else</span> <span class="keyword">if</span> (moveDelta.x &lt; <span class="number">0</span>)</span><br><span class="line">&#123;</span><br><span class="line">    transform.localScale = <span class="keyword">new</span> Vector3(<span class="number">-1</span>, <span class="number">1</span>, <span class="number">1</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"><span class="doctag">///</span> 在这里添加新代码 <span class="doctag">///</span></span></span><br><span class="line"></span><br><span class="line">transform.translate(moveDelta * Time.DeltaTime);</span><br></pre></td></tr></table></figure>
<p>然后我们说到，RaycastHit2D 的作用是放置一个投影，用于检测前方是否有障碍物，所以我们调用 <code>Physic2D.BoxCast()</code> 函数，创建一个投影块。具体检测两个轴，x 和 y。我们先检测 y 轴（上下），BoxCast() 函数需要传递 6 个参数，分别是：</p>
<ol>
<li>投影块要出现的位置：角色的位置</li>
<li>投影块的大小：角色的 BoxCollider2D 的大小</li>
<li>角度：角色移动不需要角度，所以传递 0</li>
<li>方向：所需要计算的方向（这里只能计算一个轴的方向（上下、左右））。它的计算判断应该是取 x y 轴的点，然后算与原点的角度。</li>
<li>投影块的距离：我们传递绝对值，这样不管是向左走（-1）还是向右走（1）都能为 1</li>
<li>需要检测的碰撞体所在图层：检测 Blocking 和 Actor 层</li>
</ol>
<p>当检测的投影块碰撞（collider）为空，则可以移动。代码如下</p>
<figure class="highlight c#"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 放置一个方块来检测，确保我们能够朝这个方向移动。如果检测为空，则表示可以移动</span></span><br><span class="line">hit = Physics2D.BoxCast(</span><br><span class="line">    transform.position, <span class="comment">// 角色的位置</span></span><br><span class="line">    boxCollider.size, <span class="comment">// 碰撞体的大小</span></span><br><span class="line">    <span class="number">0</span>, <span class="comment">// 角色不需要移动角度，所以为 0</span></span><br><span class="line">    <span class="keyword">new</span> Vector2(<span class="number">0</span>, moveDelta.y), <span class="comment">// 方向计算其中一个方向 moveDelta.y</span></span><br><span class="line">    Mathf.Abs(moveDelta.y * Time.deltaTime), <span class="comment">// 检测距离为我们需要移动的距离</span></span><br><span class="line">    LayerMask.GetMask(<span class="string">&quot;Actor&quot;</span>, <span class="string">&quot;Blocking&quot;</span>)); <span class="comment">// 需要检测的层</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果检测到空的</span></span><br><span class="line"><span class="keyword">if</span> (hit.collider == <span class="literal">null</span>)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// 让他动起来</span></span><br><span class="line">    <span class="comment">// 因为我们只检测 y 轴，所以我们要修改为 y 轴</span></span><br><span class="line">    transform.Translate(<span class="number">0</span>, moveDelta.y * Time.deltaTime, <span class="number">0</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>同样，我们也需要给 x 轴有同样的操作：</p>
<figure class="highlight c#"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 放置一个方块来检测，确保我们能够朝这个方向移动。如果检测为空，则表示可以移动</span></span><br><span class="line">hit = Physics2D.BoxCast(</span><br><span class="line">    transform.position, <span class="comment">// 角色的位置</span></span><br><span class="line">    boxCollider.size, <span class="comment">// 碰撞体的大小</span></span><br><span class="line">    <span class="number">0</span>, <span class="comment">// 角色不需要移动角度，所以为 0</span></span><br><span class="line">    <span class="keyword">new</span> Vector2(moveDelta.x, <span class="number">0</span>), <span class="comment">// 这个是检测 x 轴的，所以需要改</span></span><br><span class="line">    Mathf.Abs(moveDelta.x * Time.deltaTime), <span class="comment">// 这个是检测 x 轴的，所以需要改</span></span><br><span class="line">    LayerMask.GetMask(<span class="string">&quot;Actor&quot;</span>, <span class="string">&quot;Blocking&quot;</span>)); <span class="comment">// 需要检测的层</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果检测到空的</span></span><br><span class="line"><span class="keyword">if</span> (hit.collider == <span class="literal">null</span>)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// 因为检测 x 轴，所以我们要修改为 x 轴</span></span><br><span class="line">    transform.Translate(moveDelta.x * Time.deltaTime, <span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>修改后的完整代码如下</p>
<figure class="highlight c#"><table><tr><td class="code"><pre><span class="line"><span class="keyword">using</span> System.Collections;</span><br><span class="line"><span class="keyword">using</span> System.Collections.Generic;</span><br><span class="line"><span class="keyword">using</span> UnityEngine;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">Player</span> : <span class="title">MonoBehaviour</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">private</span> BoxCollider2D boxCollider;</span><br><span class="line">    <span class="keyword">private</span> Vector3 moveDelta;</span><br><span class="line">    <span class="keyword">private</span> RaycastHit2D hit; <span class="comment">// 用于碰撞检测</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Start</span>()</span></span><br><span class="line">    &#123;</span><br><span class="line">        boxCollider = GetComponent&lt;BoxCollider2D&gt;();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">FixedUpdate</span>()</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">float</span> x = Input.GetAxisRaw(<span class="string">&quot;Horizontal&quot;</span>);</span><br><span class="line">        <span class="built_in">float</span> y = Input.GetAxisRaw(<span class="string">&quot;Vertical&quot;</span>);</span><br><span class="line"></span><br><span class="line">        moveDelta = <span class="keyword">new</span> Vector3(x, y, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (moveDelta.x &gt; <span class="number">0</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            transform.localScale = Vector3.one;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> <span class="keyword">if</span> (moveDelta.x &lt; <span class="number">0</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            transform.localScale = <span class="keyword">new</span> Vector3(<span class="number">-1</span>, <span class="number">1</span>, <span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 放置一个方块来检测，确保我们能够朝这个方向移动。如果检测为空，则表示可以移动</span></span><br><span class="line">        hit = Physics2D.BoxCast(</span><br><span class="line">            transform.position, <span class="comment">// 角色的位置</span></span><br><span class="line">            boxCollider.size, <span class="comment">// 碰撞体的大小</span></span><br><span class="line">            <span class="number">0</span>, <span class="comment">// 角色不需要移动角度，所以为 0</span></span><br><span class="line">            <span class="keyword">new</span> Vector2(<span class="number">0</span>, moveDelta.y), <span class="comment">// 方向计算其中一个方向 moveDelta.y</span></span><br><span class="line">            Mathf.Abs(moveDelta.y * Time.deltaTime), <span class="comment">// 检测距离为我们需要移动的距离</span></span><br><span class="line">            LayerMask.GetMask(<span class="string">&quot;Actor&quot;</span>, <span class="string">&quot;Blocking&quot;</span>)); <span class="comment">// 需要检测的层</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 如果检测到空的</span></span><br><span class="line">        <span class="keyword">if</span> (hit.collider == <span class="literal">null</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">// 让他动起来</span></span><br><span class="line">            <span class="comment">// 因为我们只检测 y 轴，所以我们要修改为 y 轴</span></span><br><span class="line">            transform.Translate(<span class="number">0</span>, moveDelta.y * Time.deltaTime, <span class="number">0</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        </span><br><span class="line">        hit = Physics2D.BoxCast(</span><br><span class="line">            transform.position,</span><br><span class="line">            boxCollider.size,</span><br><span class="line">            <span class="number">0</span>, <span class="comment">// 角色不需要移动角度，所以为 0</span></span><br><span class="line">            <span class="keyword">new</span> Vector2(moveDelta.x, <span class="number">0</span>), <span class="comment">// 这个是检测 x 轴的，所以需要改</span></span><br><span class="line">            Mathf.Abs(moveDelta.x * Time.deltaTime), <span class="comment">// 这个是检测 x 轴的，所以需要改</span></span><br><span class="line">            LayerMask.GetMask(<span class="string">&quot;Actor&quot;</span>, <span class="string">&quot;Blocking&quot;</span>));</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (hit.collider == <span class="literal">null</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">// 因为我们检测 x 轴，所以我们要修改为 x 轴</span></span><br><span class="line">            transform.Translate(moveDelta.x * Time.deltaTime, <span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>检测的时候，我们发现玩家居然完全不能移动，怎么回事？</p>
<p>因为我们检测的是 Blocking 层和 Actor 层、玩家也在 Actor 层里，所以他就检测自己，觉得“自己和自己碰撞了”。那需要解决这个问题，我们需要打开项目设置：Unity 左上角 Edit / Project Settings 然后弹出的对话框中左侧选择 Physic 2D。在右侧的属性列表中找到 Queries Start in Colliders，取消勾选。</p>
<p><img src="/uploads/20210808%20Physic2D%20Setting.png"/></p>
<p>设置结束后，运行游戏。可以发现玩家可以在地图上奔跑，然后遇到障碍物会停下了。</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://kingsmai.github.io">小麦 Andrew Xiaomai</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://kingsmai.github.io/2021/08/09/RPG-%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91%EF%BC%884%EF%BC%89%E7%BB%98%E5%88%B6%E5%9C%B0%E6%9D%BF%E5%92%8CNPC%E5%B9%B6%E5%AE%9E%E7%8E%B0%E7%A2%B0%E6%92%9E%E6%A3%80%E6%B5%8B/">https://kingsmai.github.io/2021/08/09/RPG-游戏开发（4）绘制地板和NPC并实现碰撞检测/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow noreferrer" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://kingsmai.github.io" target="_blank">麦甜怪圈 Xiaomai Circle</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Unity-%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91/">Unity 游戏开发</a><a class="post-meta__tags" href="/tags/RPG-%E6%B8%B8%E6%88%8F/">RPG 游戏</a></div><div class="post_share"><div class="social-share" data-image="/img/covers/Unity2020rpg2d.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i>欢迎打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/wallets/WechatPaySquare.jpg" target="_blank"><img class="post-qr-code-img" src="/img/wallets/WechatPaySquare.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/wallets/AlipaySquare.jpg" target="_blank"><img class="post-qr-code-img" src="/img/wallets/AlipaySquare.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/08/09/RPG-%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91%EF%BC%883%EF%BC%89%E8%A7%92%E8%89%B2%E7%A7%BB%E5%8A%A8/" title="RPG 游戏开发（3）角色移动"><img class="cover" src="/img/covers/Unity2020rpg2d.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">RPG 游戏开发（3）角色移动</div></div></a></div><div class="next-post pull-right"><a href="/2021/08/10/RPG-%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91%EF%BC%885%EF%BC%89%E9%95%9C%E5%A4%B4%E8%B7%9F%E9%9A%8F/" title="RPG 游戏开发（5）镜头跟随"><img class="cover" src="/img/covers/Unity2020rpg2d.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">RPG 游戏开发（5）镜头跟随</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2021/08/13/RPG-%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91%EF%BC%8810%EF%BC%89%E5%8F%AF%E7%A2%B0%E6%92%9E%E7%89%A9%E4%BD%93/" title="RPG 游戏开发（10）可碰撞物体"><img class="cover" src="/img/covers/Unity2020rpg2d.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-08-13</div><div class="title">RPG 游戏开发（10）可碰撞物体</div></div></a></div><div><a href="/2021/08/14/RPG-%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91%EF%BC%8812%EF%BC%89%E8%A7%A6%E7%A2%B0%E7%AE%B1%E5%AD%90%E8%8E%B7%E5%8F%96%E9%87%91%E5%B8%81/" title="RPG 游戏开发（12）触碰箱子获取金币"><img class="cover" src="/img/covers/Unity2020rpg2d.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-08-14</div><div class="title">RPG 游戏开发（12）触碰箱子获取金币</div></div></a></div><div><a href="/2021/08/15/RPG-%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91%EF%BC%8814%EF%BC%89GameManager/" title="RPG 游戏开发（14）GameManager"><img class="cover" src="/img/covers/Unity2020rpg2d.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-08-15</div><div class="title">RPG 游戏开发（14）GameManager</div></div></a></div><div><a href="/2021/08/15/RPG-%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91%EF%BC%8815%EF%BC%89PlayerPrefs/" title="RPG 游戏开发（15）PlayerPrefs"><img class="cover" src="/img/covers/Unity2020rpg2d.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-08-15</div><div class="title">RPG 游戏开发（15）PlayerPrefs</div></div></a></div><div><a href="/2021/08/16/RPG-%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91%EF%BC%8816%EF%BC%89%E6%96%87%E5%AD%97%E7%B3%BB%E7%BB%9F-1/" title="RPG 游戏开发（16）文字系统 1"><img class="cover" src="/img/covers/Unity2020rpg2d.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-08-16</div><div class="title">RPG 游戏开发（16）文字系统 1</div></div></a></div><div><a href="/2021/08/13/RPG-%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91%EF%BC%8811%EF%BC%89CSharp-%E7%BB%A7%E6%89%BF/" title="RPG 游戏开发（11）CSharp 继承"><img class="cover" src="/img/covers/Unity2020rpg2d.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-08-13</div><div class="title">RPG 游戏开发（11）CSharp 继承</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://avatars.githubusercontent.com/u/23566051?s=400&amp;u=cc8834557b9fe7ca3db3adc89606ae47503e603d&amp;v=4" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">小麦 Andrew Xiaomai</div><div class="author-info__description">一个喜欢肉鸽类游戏的程序员<br>分享日常生活，学习笔记，技术文章等。感兴趣想一起开发游戏的欢迎联系我哟！</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">54</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">37</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">28</div></a></div><a id="card-info-btn" target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/kingsmai"><i class="fab fa-github"></i><span>关注我</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/kingsmai" rel="external nofollow noreferrer" target="_blank" title="Github"><i class="iconfont icon-GitHub" style="color: #24292e;"></i></a><a class="social-icon" href="https://space.bilibili.com/670118055" rel="external nofollow noreferrer" target="_blank" title="BiliBili"><i class="iconfont icon-bilibili-fill" style="color: #ff6699;"></i></a><a class="social-icon" href="mailto:xsbugh@gmail.com" rel="external nofollow noreferrer" target="_blank" title="Email"><i class="iconfont icon-email-fill" style="color: #ff3e30;"></i></a><a class="social-icon" href="https://www.zhihu.com/people/creeper0924" rel="external nofollow noreferrer" target="_blank" title="Zhihu"><i class="iconfont icon-zhihu" style="color: #1772F6;"></i></a><a class="social-icon" href="https://blog.csdn.net/Xsbugh" rel="external nofollow noreferrer" target="_blank" title="CSDN"><i class="iconfont icon-csdn" style="color: #FC4144;"></i></a><a class="social-icon" href="https://www.linkedin.com/in/%E5%B0%8F%E9%BA%A6-xiaomai-0a672124b/" rel="external nofollow noreferrer" target="_blank" title="LinkedIn"><i class="iconfont icon-linkedin" style="color: #0A66C2;"></i></a><a class="social-icon" href="/atom.xml" target="_blank" title="RSS 链接"><i class="iconfont icon-Atom" style="color: #6EA3E5;"></i></a><a class="social-icon" href="/rss2.xml" target="_blank" title="RSS 链接"><i class="iconfont icon-dingyue" style="color: #ee802f;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="card-widget"><div class="item-headline"><i></i><span></span></div><div class="item-content"></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%B6%E4%BD%9C%E5%9C%B0%E6%9D%BF"><span class="toc-number">1.</span> <span class="toc-text">制作地板</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9%E6%B8%B2%E6%9F%93%E5%9B%BE%E5%B1%82"><span class="toc-number">2.</span> <span class="toc-text">修改渲染图层</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%A2%B0%E6%92%9E%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="toc-number">3.</span> <span class="toc-text">碰撞的概念</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BC%96%E7%A8%8B"><span class="toc-number">4.</span> <span class="toc-text">编程</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/12/20/ML-8-%E6%9E%84%E5%BB%BA%E9%80%BB%E8%BE%91%E5%9B%9E%E5%BD%92%E6%A8%A1%E5%9E%8B/" title="Sololearn 自学机器学习（8）构建逻辑回归模型"><img src="/img/covers/sklearn-build-linear-regression-model.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Sololearn 自学机器学习（8）构建逻辑回归模型"/></a><div class="content"><a class="title" href="/2023/12/20/ML-8-%E6%9E%84%E5%BB%BA%E9%80%BB%E8%BE%91%E5%9B%9E%E5%BD%92%E6%A8%A1%E5%9E%8B/" title="Sololearn 自学机器学习（8）构建逻辑回归模型">Sololearn 自学机器学习（8）构建逻辑回归模型</a><time datetime="2023-12-19T16:15:57.000Z" title="发表于 2023-12-20 00:15:57">2023-12-20</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/12/19/ML-7-%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92%E6%A8%A1%E5%9E%8B/" title="Sololearn 自学机器学习（7）线性回归模型"><img src="/img/covers/linear-regression-3.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Sololearn 自学机器学习（7）线性回归模型"/></a><div class="content"><a class="title" href="/2023/12/19/ML-7-%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92%E6%A8%A1%E5%9E%8B/" title="Sololearn 自学机器学习（7）线性回归模型">Sololearn 自学机器学习（7）线性回归模型</a><time datetime="2023-12-19T08:07:36.000Z" title="发表于 2023-12-19 16:07:36">2023-12-19</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/12/19/ML-6-%E5%88%86%E7%B1%BB-Classification/" title="Sololearn 自学机器学习（6）分类 Classification"><img src="/img/covers/classifications-3.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Sololearn 自学机器学习（6）分类 Classification"/></a><div class="content"><a class="title" href="/2023/12/19/ML-6-%E5%88%86%E7%B1%BB-Classification/" title="Sololearn 自学机器学习（6）分类 Classification">Sololearn 自学机器学习（6）分类 Classification</a><time datetime="2023-12-19T05:36:19.000Z" title="发表于 2023-12-19 13:36:19">2023-12-19</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/12/19/ML-5-Matplotlib-%E7%BB%98%E5%9B%BE%E5%9F%BA%E7%A1%80/" title="Sololearn 自学机器学习（5）Matplotlib 绘图基础"><img src="/img/covers/matplotlib-plot-basic-5.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Sololearn 自学机器学习（5）Matplotlib 绘图基础"/></a><div class="content"><a class="title" href="/2023/12/19/ML-5-Matplotlib-%E7%BB%98%E5%9B%BE%E5%9F%BA%E7%A1%80/" title="Sololearn 自学机器学习（5）Matplotlib 绘图基础">Sololearn 自学机器学习（5）Matplotlib 绘图基础</a><time datetime="2023-12-18T17:08:34.000Z" title="发表于 2023-12-19 01:08:34">2023-12-19</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/12/18/ML-4-Numpy-%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86/" title="Sololearn 自学机器学习（4）Numpy 数据处理"><img src="/img/covers/numpy-data-processing-2.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Sololearn 自学机器学习（4）Numpy 数据处理"/></a><div class="content"><a class="title" href="/2023/12/18/ML-4-Numpy-%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86/" title="Sololearn 自学机器学习（4）Numpy 数据处理">Sololearn 自学机器学习（4）Numpy 数据处理</a><time datetime="2023-12-18T13:26:11.000Z" title="发表于 2023-12-18 21:26:11">2023-12-18</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/img/covers/Unity2020rpg2d.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2019 - 2023 By 小麦 Andrew Xiaomai</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener external nofollow noreferrer" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><a href="https://icp.gov.moe/?keyword=20235160" rel="external nofollow noreferrer" target="_blank"><img class="icp-icon" src="/img/moe-icons/icon120.png">备案号：✮萌ICP备20235160号✮</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><div class="js-pjax"></div><canvas class="fireworks" mobile="true"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/fireworks.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-fluttering-ribbon.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div></body></html>